@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
@using System.Collections.Generic
@using System.Linq
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
@{
    var namespaceSet = new HashSet<string>(
        new string[]
        {
            "Microsoft.AspNetCore.Mvc",
            "Microsoft.AspNetCore.Mvc.RazorPages",
            "Microsoft.AspNetCore.Identity",
            "Microsoft.Extensions.Logging",
        });
    var thisNamespace = $"{Model.Namespace}.Areas.Identity.Pages.Account.Manage";
    if (Model.IsGenerateCustomUser)
    {
        if (thisNamespace != Model.UserClassNamespace)
        {
            namespaceSet.Add(Model.UserClassNamespace);
        }
    }
    foreach(var name in namespaceSet.OrderBy(n => n))
    {
@:using @name;
    }
}

namespace @thisNamespace
{
    public class DeletePersonalDataModel : PageModel
    {
        private readonly UserManager<@(Model.UserClass)> _userManager;
        private readonly SignInManager<@(Model.UserClass)> _signInManager;
        private readonly ILogger<DeletePersonalDataModel> _logger;

        public DeletePersonalDataModel(
            UserManager<@(Model.UserClass)> userManager,
            SignInManager<@(Model.UserClass)> signInManager,
            ILogger<DeletePersonalDataModel> logger)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
        }

        [BindProperty]
        public InputModel Input { get; set; }

        public class InputModel
        {
            [Required]
            [DataType(DataType.Password)]
            public string Password { get; set; }
        }

        public bool RequirePassword { get; set; }

        public async Task<IActionResult> OnGet()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            var user = await _userManager.GetUserAsync(User);
            if (user == null)
            {
                return NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            RequirePassword = await _userManager.HasPasswordAsync(user);
            if (RequirePassword)
            {
                if (!await _userManager.CheckPasswordAsync(user, Input.Password))
                {
                    ModelState.AddModelError(string.Empty, "Password not correct.");
                    return Page();
                }
            }

            var result = await _userManager.DeleteAsync(user);
            if (!result.Succeeded)
            {
                throw new InvalidOperationException($"Unexpected error occurred deleteing user with ID '{user.Id}'.");
            }

            await _signInManager.SignOutAsync();

            _logger.LogInformation("User with ID '{UserId}' deleted themselves.", _userManager.GetUserId(User));

            return Redirect("~/");
        }
    }
}